package com.haidechizi.i18n;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.haidechizi.i18n.common.api.I18nService;
import com.haidechizi.i18n.common.api.vo.BusinessLanguageVO;
import com.haidechizi.i18n.common.api.vo.MultiLanguage;
import com.haidechizi.i18n.common.api.vo.SingleLanguage;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 模仿国际化接口
 */
//@Service
public class I18nServiceImpl implements I18nService {

    private Map<String, SingleLanguage> singleMap = new HashMap<>();

    @PostConstruct
    private void init() {
        SingleLanguage singleLanguage = new SingleLanguage();
        singleLanguage.setSystemCode("i18n-wev-demo");
        singleLanguage.setBusinessCode("student");
        singleLanguage.setBusinessKey("1");
        singleLanguage.setLanguageName("en_US");
        singleLanguage.setIsDefault(true);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "Mic");
        jsonObject.put("clazz", "class 1");
        singleLanguage.setDocJson(jsonObject.toJSONString());
        String key = generateKey(singleLanguage.getSystemCode(), singleLanguage.getBusinessCode(),
                singleLanguage.getLanguageName(), singleLanguage.getBusinessKey());
        singleMap.put(key, singleLanguage);
    }


    @Override
    public Boolean addBusinessLanguages(List<BusinessLanguageVO> businessLanguageVOs) {
        return null;
    }

    @Override
    public SingleLanguage querySingleLanguage(String systemCode, String businessCode, String languageName, String businessKey) {
        return null;
    }

    @Override
    public MultiLanguage queryMutilLanguage(String systemCode, String businessCode, String businessKey) {
        return null;
    }

    @Override
    public List<SingleLanguage> batchQuerySingleLanguage(String systemCode, String businessCode, String languageName, List<String> businessKeyList) {
        String key = generateKey(systemCode, businessCode, languageName, businessKeyList.get(0));
        if (!singleMap.containsKey(key)) {
            return Lists.newArrayList();
        }
        return Lists.newArrayList(singleMap.get(key));
    }

    @Override
    public List<MultiLanguage> batchQueryMutilLanguage(String systemCode, String businessCode, List<String> businessKeyList) {
        return null;
    }

    @Override
    public Map<String, String> supportLanguages(String systemCode, String businessCode) {
        return null;
    }

    private String generateKey(String systemCode, String businessCode, String languageName, String id) {
        StringBuilder sb = new StringBuilder();
        sb.append(systemCode).append(":").append(businessCode)
                .append(":").append(languageName).append(":").append(id);
        return sb.toString();
    }
}
